package com.ruoyi.web.controller.owner;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.owner.domain.FzBuildingOwnerRoomRel;
import com.ruoyi.owner.domain.FzOwnerDetails;
import com.ruoyi.owner.service.IFzBuildingOwnerRoomRelService;
import com.ruoyi.owner.service.IFzOwnerDetailsService;
import com.ruoyi.system.domain.assets.FzBuildingRoom;
import com.ruoyi.system.domain.assets.FzBuildingUnit;
import com.ruoyi.system.domain.assets.FzBusinessFloor;
import com.ruoyi.system.domain.carPark.FzCarPark;
import com.ruoyi.system.domain.carPark.FzCarlot;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.assets.IFzBuildingRoomService;
import com.ruoyi.system.service.assets.IFzBuildingUnitService;
import com.ruoyi.system.service.assets.IFzBusinessFloorService;
import com.ruoyi.system.service.carPark.IFzCarParkService;
import com.ruoyi.system.service.carPark.IFzCarlotService;
import com.ruoyi.web.controller.owner.vo.FzBuildingRoomVo;
import com.ruoyi.web.controller.owner.vo.FzCarlotVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import sun.security.acl.OwnerImpl;

/**
 * 业主成员Controller
 * 
 * @author ruoyi
 * @date 2020-03-09
 */
@Controller
@RequestMapping("/owner/rel")
public class FzBuildingOwnerRoomRelController extends BaseController
{
    private String prefix = "owner/rel";

    @Autowired
    private IFzBuildingOwnerRoomRelService fzBuildingOwnerRoomRelService;

    @Autowired
    private IFzOwnerDetailsService iFzOwnerDetailsService;

    @Autowired
    private IFzBuildingRoomService fzBuildingRoomService;

    @Autowired
    private IFzCarlotService fzCarlotService;

    @Autowired
    private IFzBusinessFloorService fzBusinessFloorService;

    @Autowired
    private IFzBuildingUnitService fzBuildingUnitService;

    private IFzCarParkService fzCarParkService;
    @RequiresPermissions("owner:rel:view")
    @GetMapping()
    public String rel()
    {
        return prefix + "/rel";
    }

    @GetMapping("/details")
    public String details(@RequestParam("ownerId")String ownerId,ModelMap map)
    {
        FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel=fzBuildingOwnerRoomRelService.selectFzBuildingOwnerRoomRelById(ownerId);
        map.put("roomrel",fzBuildingOwnerRoomRel);
        map.put("roomList",null);
        map.put("carLotList",null);
        FzOwnerDetails fzOwnerDetails=new FzOwnerDetails();
        fzOwnerDetails.setcId(GetSessionValueUtil.getCommunity());
        fzOwnerDetails.setOwnerId(ownerId);
        List<FzOwnerDetails> fzOwnerDetailses=iFzOwnerDetailsService.selectFzOwnerDetailsList(fzOwnerDetails);
        if (!CollectionUtils.isEmpty(fzOwnerDetailses)){
                List<String> room=fzOwnerDetailses.stream().filter((FzOwnerDetails fzOwnerDetailse)->{
                    if(fzOwnerDetailse.getAssetsType()==0) return true;
                    return false; }).map(FzOwnerDetails::getDetailsId).collect(Collectors.toList());
                    List<String> car=fzOwnerDetailses.stream().filter((FzOwnerDetails fzOwnerDetailse)->{
                    if(fzOwnerDetailse.getAssetsType()==1) return true;
                    return false; }).map(FzOwnerDetails::getDetailsId).collect(Collectors.toList());
                        if(!CollectionUtils.isEmpty(room)){
                            List<FzBuildingRoomVo> fzBuildingRoomVos= new ArrayList<>();
                            //c查询出房屋信息
                           List<FzBuildingRoom> fzBuildingRoomList = fzBuildingRoomService.selectFzBuildingRoomByIdList(room);
                            FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
                            fzBusinessFloor.setcId(GetSessionValueUtil.getCommunity());
                            FzBuildingUnit fzBuildingUnit=new FzBuildingUnit();
                            fzBuildingUnit.setcId(GetSessionValueUtil.getCommunity());
                            //查询出房屋楼栋信息
                           List<FzBusinessFloor> fzBusinessFloors=fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
                           List<FzBuildingUnit> fzBuildingUnits=fzBuildingUnitService.selectFzBuildingUnitList(fzBuildingUnit);
                           //把信息以id为键放到Map中
                            Map<String,FzBusinessFloor> fzBusinessFloorMap= fzBusinessFloors.stream().collect(Collectors.toMap(FzBusinessFloor :: getFloorId,FzBusinessFloor->FzBusinessFloor));
                            Map<String,FzBuildingUnit> fzBuildingUnitMap=fzBuildingUnits.stream().collect(Collectors.toMap(FzBuildingUnit::getUnitId,FzBuildingUnit -> FzBuildingUnit));
                                    if(!CollectionUtils.isEmpty(fzBusinessFloorMap)&&!CollectionUtils.isEmpty(fzBuildingUnitMap)){
                                        for(FzBuildingRoom item: fzBuildingRoomList){
                                            FzBuildingRoomVo fzBuildingRoomVo=new FzBuildingRoomVo();
                                            BeanUtil.copyProperties(item,fzBuildingRoomVo);
                                            if( fzBusinessFloorMap.containsKey(fzBuildingRoomVo.getfId())&& fzBuildingUnitMap.containsKey(fzBuildingRoomVo.getUnitId())){
                                                fzBuildingRoomVo.setFloornum(fzBusinessFloorMap.get(fzBuildingRoomVo.getfId()).getFloorNum());
                                                fzBuildingRoomVo.setUnitnum(fzBuildingUnitMap.get(fzBuildingRoomVo.getUnitId()).getUnitNum());
                                                fzBuildingRoomVos.add(fzBuildingRoomVo);
                                            }
                                        }
                                    }
                           map.put("roomList", fzBuildingRoomVos);
                        }
                        if(!CollectionUtils.isEmpty(car)){
                            List<FzCarlot> carlotList=fzCarlotService.selectFzCarlotListByListId(car);
                            List<FzCarlotVo> fzCarlotVos=new ArrayList<>();
                            FzCarPark fzCarPark=new FzCarPark();
                            fzCarPark.setcId(GetSessionValueUtil.getCommunity());
                            List<FzCarPark> fzCarParkList= fzCarParkService.selectFzCarParkList(fzCarPark);
                            Map<String,FzCarPark> fzCarlotMap=fzCarParkList.stream().collect(Collectors.toMap(FzCarPark :: getCarParkId,FzCarPark->FzCarPark));
                            if(!CollectionUtils.isEmpty(fzCarlotMap)){
                                for(FzCarlot item: carlotList){
                                    FzCarlotVo fzCarlotVo=new FzCarlotVo();
                                    BeanUtil.copyProperties(item,fzCarlotVo);
                                    if( fzCarlotMap.containsKey(fzCarlotVo.getCarParkId())){
                                        fzCarlotVo.setCarParkNum(fzCarlotMap.get(fzCarlotVo.getCarParkId()).getCarParkNum());
                                        fzCarlotVos.add(fzCarlotVo);
                                    }
                                }
                            }
                            map.put("carLotList",fzCarlotVos);
                        }
        }
       return prefix + "/details";
    }

    /**
     * 查询业主成员列表
     */
    @RequiresPermissions("owner:rel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel)
    {
        startPage();
        fzBuildingOwnerRoomRel.setcId(GetSessionValueUtil.getCommunity());
        List<FzBuildingOwnerRoomRel> list = fzBuildingOwnerRoomRelService.selectFzBuildingOwnerRoomRelList(fzBuildingOwnerRoomRel);
        return getDataTable(list);
    }

    /**
     * 导出业主成员列表
     */
    @RequiresPermissions("owner:rel:export")
    @Log(title = "业主成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel)
    {
        List<FzBuildingOwnerRoomRel> list = fzBuildingOwnerRoomRelService.selectFzBuildingOwnerRoomRelList(fzBuildingOwnerRoomRel);
        ExcelUtil<FzBuildingOwnerRoomRel> util = new ExcelUtil<FzBuildingOwnerRoomRel>(FzBuildingOwnerRoomRel.class);
        return util.exportExcel(list, "rel");
    }

    /**
     * 新增业主成员
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存业主成员
     */
    @RequiresPermissions("owner:rel:add")
    @Log(title = "业主成员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel)
    {
        fzBuildingOwnerRoomRel.setOwnerId(HeadNum.OWNER_HEAD+ DateUtils.getTimeMillis());
        fzBuildingOwnerRoomRel.setcId(GetSessionValueUtil.getCommunity());
        fzBuildingOwnerRoomRel.setCreateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingOwnerRoomRelService.insertFzBuildingOwnerRoomRel(fzBuildingOwnerRoomRel));
    }

    /**
     * 修改业主成员
     */
    @GetMapping("/edit/{ownerId}")
    public String edit(@PathVariable("ownerId") String ownerId, ModelMap mmap)
    {
        FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel = fzBuildingOwnerRoomRelService.selectFzBuildingOwnerRoomRelById(ownerId);
        mmap.put("fzBuildingOwnerRoomRel", fzBuildingOwnerRoomRel);
        return prefix + "/edit";
    }

    /**
     * 修改保存业主成员
     */
    @RequiresPermissions("owner:rel:edit")
    @Log(title = "业主成员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel)
    {
        fzBuildingOwnerRoomRel.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingOwnerRoomRelService.updateFzBuildingOwnerRoomRel(fzBuildingOwnerRoomRel));
    }

    /**
     * 删除业主成员
     */
    @RequiresPermissions("owner:rel:remove")
    @Log(title = "业主成员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzBuildingOwnerRoomRelService.deleteFzBuildingOwnerRoomRelByIds(ids));
    }


    /**
     * 入住房屋
     */
    @GetMapping("/checkInRoom/{ownerId}")
    public String checkInRoom(@PathVariable("ownerId") String ownerId, ModelMap mmap)
    {
        FzBuildingOwnerRoomRel fzBuildingOwnerRoomRel=fzBuildingOwnerRoomRelService.selectFzBuildingOwnerRoomRelById(ownerId);
        mmap.put("fzBuildingOwnerRoomRel", fzBuildingOwnerRoomRel);

        FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
        fzBusinessFloor.setcId(GetSessionValueUtil.getCommunity());
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        mmap.put("floorSelectAdd",list);
        return prefix + "/checkInRoom";
    }

    //入住房屋初始化
    @PostMapping( "/checkInRoom")
    @ResponseBody
    public String checkInRoom(ModelMap mmap)
    {
        return "";
    }

    @Autowired
    private IFzOwnerDetailsService fzOwnerDetailsService;
    @Autowired
    private IFzBuildingRoomService iFzBuildingRoomService;
    //保存详细信息
    @GetMapping("/detail")
    @ResponseBody
    public AjaxResult detailSave(FzOwnerDetails fzOwnerDetails)
    {
        fzOwnerDetails.setCreateBy(ShiroUtils.getSysUser().getUserName());
        fzOwnerDetails.setcId(GetSessionValueUtil.getCommunity());
        int d=fzOwnerDetailsService.insertFzOwnerDetails(fzOwnerDetails);
        AjaxResult ajaxResult=toAjax(d);
        if(fzOwnerDetails.getAssetsType()==0 && d>0){
            FzBuildingRoom fzBuildingRoom=new FzBuildingRoom();
            fzBuildingRoom.setRoomId(fzOwnerDetails.getDetailsId());
            fzBuildingRoom.setState("0");
            iFzBuildingRoomService.updateFzBuildingRoom(fzBuildingRoom);
        }
        return ajaxResult;
    }
}
